/*
Copyright (C) 2010- Peter Bui and Aaron Dingler.
This software is distributed under the GNU General Public License.
See the file COPYING for details.
*/

#include "smq_common.h"
#include "smq_message.h"

#include <debug.h>
#include <int_sizes.h>
#include <list.h>
#include <nvpair.h>
#include <stringtools.h>
#include <xmalloc.h>

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>

/* Public functions -------------------------------------------------------- */

struct nvpair *
smq_message_read_header(const char *header_file)
{
    FILE *fp = NULL;
    struct nvpair *meta = NULL;

    meta = nvpair_create();
    if (!meta) goto smq_mrh_failure;

    fp = fopen(header_file, "r");
    if (!fp) goto smq_mrh_failure;

    if (!nvpair_parse_stream(meta, fp)) {
	debug(D_NOTICE, "smq_message_read_header: failed to parse stream");
	goto smq_mrh_failure;
    }
    fclose(fp); fp = NULL;
    
    if (!nvpair_lookup_string(meta, "source")) {
	debug(D_NOTICE, "smq_message_read_header: missing source");
	goto smq_mrh_failure;
    }
    if (!nvpair_lookup_string(meta, "target")) {
	debug(D_NOTICE, "smq_message_read_header: missing target");
	goto smq_mrh_failure;
    }

    return meta;

smq_mrh_failure:
    if (fp) fclose(fp);
    if (meta) nvpair_delete(meta);
    return NULL;
}

/* 
vim: sw=4 sts=4 ts=8 ft=cpp
*/
